package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import com.dotstone.chipism.R;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FreeDeviceListActivity extends BaseActivity {
	private LinearLayout mBackL;
	private Button mNextB;
	private TextView tv_statusBar_mian;
	private Button mMealB1;
	private Button mMealB2;
	private Button mMealB3;
	private EditText mCountEd1;
	private EditText mCountEd2;
	private EditText mCountEd3;
	private EditText mCountEd4;
	private EditText mCountEd5;
	private Button mPlusB1;
	private Button mPlusB2;
	private Button mPlusB3;
	private Button mPlusB4;
	private Button mPlusB5;
	private Button mMinusB1;
	private Button mMinusB2;
	private Button mMinusB3;
	private Button mMinusB4;
	private Button mMinusB5;
	private int count1 = 0;
	private int count2 = 0;
	private int count3 = 0;
	private int count4 = 0;
	private int count5 = 0;
	private List<EditText> mCountEds = new ArrayList<EditText>();
	private int[] meal1 = { 1, 2, 3, 4, 5 };
	private int[] meal2 = { 5, 1, 12, 4, 6 };
	private int[] meal3 = { 2, 4, 5, 6, 8 };
	private boolean clickMeal = false;
	public static FreeDeviceListActivity freeDeviceListActivity;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.nextstep:
			if (count1 != 0 || count2 != 0 || count3 != 0 || count4 != 0 || count5 != 0) {
				Intent intent = new Intent(getApplicationContext(), PublishOrderActivity.class);
				String explain = "";
				if (count1 != 0) {
					explain += "智能网关" + count1 + "个；";
				}
				if (count2 != 0) {
					explain += "智能插座" + count2 + "个；";
				}
				if (count3 != 0) {
					explain += "智能开关" + count3 + "个；";
				}
				if (count4 != 0) {
					explain += "智能彩灯" + count4 + "个；";
				}
				if (count5 != 0) {
					explain += "智能门锁" + count5 + "个；";
				}
				Log.i(TAG, "explain" + explain);
				intent.putExtra("explain", explain);
				startActivity(intent);
			} else {
				ToastShow.Show(getApplicationContext(), getString(R.string.device_count));
			}
			break;
		case R.id.meal_b1:
			setCount(1);
			break;
		case R.id.meal_b2:
			setCount(2);
			break;
		case R.id.meal_b3:
			setCount(3);
			break;
		case R.id.plus_b1:
			if (clickMeal) {
				resetMealView();
			}
			count1++;
			mCountEd1.setText("" + count1);
			if (mCountEd1.hasFocus()) {
				mCountEd1.setSelection(("" + count1).length());
			}
			// mCountEd1.setSelection(("" + count1).length());
			break;
		case R.id.minus_b1:
			if (clickMeal) {
				resetMealView();
			}
			count1 = Integer.parseInt(mCountEd1.getText().toString());
			if (count1 > 0) {
				count1--;
				mCountEd1.setText("" + count1);
				if (mCountEd1.hasFocus()) {
					mCountEd1.setSelection(("" + count1).length());
				}
			} else {
			}
			break;
		case R.id.plus_b2:
			if (clickMeal) {
				resetMealView();
			}
			count2++;
			mCountEd2.setText("" + count2);
			if (mCountEd2.hasFocus()) {
				mCountEd2.setSelection(("" + count2).length());
			}
			break;
		case R.id.minus_b2:
			if (clickMeal) {
				resetMealView();
			}
			count2 = Integer.parseInt(mCountEd2.getText().toString());
			if (count2 > 0) {
				count2--;
				mCountEd2.setText("" + count2);
				if (mCountEd2.hasFocus()) {
					mCountEd2.setSelection(("" + count2).length());
				}
			} else {

			}
			break;
		case R.id.plus_b3:
			if (clickMeal) {
				resetMealView();
			}
			count3++;
			mCountEd3.setText("" + count3);
			if (mCountEd3.hasFocus()) {
				mCountEd3.setSelection(("" + count3).length());
			}
			break;
		case R.id.minus_b3:
			if (clickMeal) {
				resetMealView();
			}
			count3 = Integer.parseInt(mCountEd3.getText().toString());
			if (count3 > 0) {
				count3--;
				mCountEd3.setText("" + count3);
				if (mCountEd3.hasFocus()) {
					mCountEd3.setSelection(("" + count3).length());
				}
			} else {
			}
			break;
		case R.id.plus_b4:
			if (clickMeal) {
				resetMealView();
			}
			count4++;
			mCountEd4.setText("" + count4);
			if (mCountEd4.hasFocus()) {
				mCountEd4.setSelection(("" + count4).length());
			}
			break;
		case R.id.minus_b4:
			if (clickMeal) {
				resetMealView();
			}
			count4 = Integer.parseInt(mCountEd4.getText().toString());
			if (count4 > 0) {
				count4--;
				mCountEd4.setText("" + count4);
				if (mCountEd4.hasFocus()) {
					mCountEd4.setSelection(("" + count4).length());
				}
			} else {
			}
			break;
		case R.id.plus_b5:
			if (clickMeal) {
				resetMealView();
			}
			count5++;
			mCountEd5.setText("" + count5);
			if (mCountEd5.hasFocus()) {
				mCountEd5.setSelection(("" + count5).length());
			}
			break;
		case R.id.minus_b5:
			if (clickMeal) {
				resetMealView();
			}
			count5 = Integer.parseInt(mCountEd5.getText().toString());
			if (count5 > 0) {
				count5--;
				mCountEd5.setText("" + count5);
				if (mCountEd5.hasFocus()) {
					mCountEd5.setSelection(("" + count5).length());
				}
			} else {
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_free_device_list;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		freeDeviceListActivity = this;
		mBackL = $(R.id.back_layout);
		mNextB = $(R.id.nextstep);
		mNextB.setVisibility(View.VISIBLE);
		tv_statusBar_mian = $(R.id.tv_statusBar_mian);
		mMealB1 = $(R.id.meal_b1);
		mMealB2 = $(R.id.meal_b2);
		mMealB3 = $(R.id.meal_b3);
		mCountEd1 = $(R.id.count_ed1);
		mCountEd2 = $(R.id.count_ed2);
		mCountEd3 = $(R.id.count_ed3);
		mCountEd4 = $(R.id.count_ed4);
		mCountEd5 = $(R.id.count_ed5);
		mPlusB1 = $(R.id.plus_b1);
		mPlusB2 = $(R.id.plus_b2);
		mPlusB3 = $(R.id.plus_b3);
		mPlusB4 = $(R.id.plus_b4);
		mPlusB5 = $(R.id.plus_b5);
		mMinusB1 = $(R.id.minus_b1);
		mMinusB2 = $(R.id.minus_b2);
		mMinusB3 = $(R.id.minus_b3);
		mMinusB4 = $(R.id.minus_b4);
		mMinusB5 = $(R.id.minus_b5);
		mCountEds.add(mCountEd1);
		mCountEds.add(mCountEd2);
		mCountEds.add(mCountEd3);
		mCountEds.add(mCountEd4);
		mCountEds.add(mCountEd5);
		mCountEd1.setText("" + count1);
		mCountEd2.setText("" + count2);
		mCountEd3.setText("" + count3);
		mCountEd4.setText("" + count4);
		mCountEd5.setText("" + count5);
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				initData();
			}
		}).start();
		mBackL.setOnClickListener(this);
		mNextB.setOnClickListener(this);
		mMealB1.setOnClickListener(this);
		mMealB2.setOnClickListener(this);
		mMealB3.setOnClickListener(this);
		mPlusB1.setOnClickListener(this);
		mPlusB2.setOnClickListener(this);
		mPlusB3.setOnClickListener(this);
		mPlusB4.setOnClickListener(this);
		mPlusB5.setOnClickListener(this);
		mMinusB1.setOnClickListener(this);
		mMinusB2.setOnClickListener(this);
		mMinusB3.setOnClickListener(this);
		mMinusB4.setOnClickListener(this);
		mMinusB5.setOnClickListener(this);
		mCountEd1.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					if (mCountEd1.getText().toString().equals("0")) {
						mCountEd1.setText("");
					}
				} else {
					if (mCountEd1.getText().toString().length() == 0) {
						mCountEd1.setText("0");
						// mMinusB1.setEnabled(false);
					}
					count1 = Integer.parseInt(mCountEd1.getText().toString());
				}
			}
		});
		mCountEd2.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					if (mCountEd2.getText().toString().equals("0")) {
						mCountEd2.setText("");
					}
				} else {
					if (mCountEd2.getText().toString().length() == 0) {
						mCountEd2.setText("0");
						// mMinusB2.setEnabled(false);
					}
					count2 = Integer.parseInt(mCountEd2.getText().toString());
				}
			}
		});
		mCountEd3.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					if (mCountEd3.getText().toString().equals("0")) {
						mCountEd3.setText("");
					}
				} else {
					if (mCountEd3.getText().toString().length() == 0) {
						mCountEd3.setText("0");
						// mMinusB3.setEnabled(false);
					}
					count3 = Integer.parseInt(mCountEd3.getText().toString());
				}
			}
		});
		mCountEd4.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					if (mCountEd4.getText().toString().equals("0")) {
						mCountEd4.setText("");
					}
				} else {
					if (mCountEd4.getText().toString().length() == 0) {
						mCountEd4.setText("0");
						// mMinusB4.setEnabled(false);
					}
					count4 = Integer.parseInt(mCountEd4.getText().toString());
				}
			}
		});
		mCountEd5.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					if (mCountEd5.getText().toString().equals("0")) {
						mCountEd5.setText("");
					}
				} else {
					if (mCountEd5.getText().toString().length() == 0) {
						mCountEd5.setText("0");
						// mMinusB5.setEnabled(false);
					}
					count5 = Integer.parseInt(mCountEd5.getText().toString());
				}
			}
		});
		mCountEd1.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable e) {
				// TODO Auto-generated method stub
				Log.i("wmy", count5 + " " + mCountEd5.getText().toString());
				if (mCountEd1.getText().toString().length() != 0) {
					if (count1 != Integer.parseInt(mCountEd1.getText().toString())) {
						if (clickMeal) {
							resetMealView();
						}
					}
					count1 = Integer.parseInt(mCountEd1.getText().toString());
				}
			}
		});
		mCountEd2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable e) {
				// TODO Auto-generated method stub
				Log.i("wmy", count2 + " " + mCountEd2.getText().toString());
				if (mCountEd2.getText().toString().length() != 0) {
					if (count2 != Integer.parseInt(mCountEd2.getText().toString())) {
						if (clickMeal) {
							resetMealView();
						}
					}
					count2 = Integer.parseInt(mCountEd2.getText().toString());
				}
			}
		});
		mCountEd3.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable e) {
				// TODO Auto-generated method stub
				Log.i("wmy", count3 + " " + mCountEd3.getText().toString());
				if (mCountEd3.getText().toString().length() != 0) {
					if (count3 != Integer.parseInt(mCountEd3.getText().toString())) {
						if (clickMeal) {
							resetMealView();
						}
					}
					count3 = Integer.parseInt(mCountEd3.getText().toString());
				}
			}
		});
		mCountEd4.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable e) {
				// TODO Auto-generated method stub
				Log.i("wmy", count4 + " " + mCountEd4.getText().toString());
				if (mCountEd4.getText().toString().length() != 0) {
					if (count4 != Integer.parseInt(mCountEd4.getText().toString())) {
						if (clickMeal) {
							resetMealView();
						}
					}
					count4 = Integer.parseInt(mCountEd4.getText().toString());
				}
			}
		});
		mCountEd5.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable e) {
				// TODO Auto-generated method stub
				Log.i("wmy", count5 + " " + mCountEd5.getText().toString());
				if (mCountEd5.getText().toString().length() != 0) {
					if (count5 != Integer.parseInt(mCountEd5.getText().toString())) {
						if (clickMeal) {
							resetMealView();
						}
					}
					count5 = Integer.parseInt(mCountEd5.getText().toString());
				}
			}
		});
		boolVersion();
	}

	private void initData() {
		RequestParams params = new RequestParams("http://api.shop.soochowlightpower.com/v2/xin.order.list");
		params.addHeader("X-ECAPI-Authorization", (String) SPUtils.get(getApplicationContext(), "shopToken", ""));
		Log.i("wmy", "shopToken = "+(String) SPUtils.get(getApplicationContext(), "shopToken", ""));
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
//			obj.put("user_id", SocketManager.getInstance().user_id);
			obj.put("status", 0);
			obj.put("per_page", 10);
			obj.put("page", 1);
			 Log.e("wmy", " get all dingdan = " + obj.toString()+" "+params.getHeaders().get("X-ECAPI-Authorization"));
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				 Log.e("wmy", "get all dingdan=" + result);
				 JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(result);
					JSONObject jo = jsonObject.getJSONObject("data");
					JSONArray ja = jo.getJSONArray("orders");
					if(ja.length()>0){
						Log.i("wmy", "有订单存在，可以发布工单");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onCancelled(CancelledException arg0) {
				 Log.e("wmy", "get all dingdan cancel");
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				// lt.error();
				 Log.e("wmy", "get all dingdan error");
			}

			@Override
			public void onFinished() {
				Log.e("wmy", "get all dingdan finish");
			}
		});
//	}
//		CloseableHttpClient httpclient = HttpClients.createDefault();
//		HttpPost httpPost = new HttpPost("api.shop.soochowlightpower.com/v2/xin.order.list");
//		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//		NameValuePair name = new BasicNameValuePair("page", "-1");
//		NameValuePair name1 = new BasicNameValuePair("per_page", "0");
//		NameValuePair name2 = new BasicNameValuePair("status", "0");
//		nvps.add(name);
//		nvps.add(name1);
//		nvps.add(name2);
//		try {
//			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//			httpPost.setHeader("X-ECAPI-Authorization", (String) SPUtils.get(getApplicationContext(), "shopToken", ""));
//
//			CloseableHttpResponse response = httpclient.execute(httpPost);
//			HttpEntity entity = response.getEntity();
//			if(entity!=null){
//				String body = EntityUtils.toString(entity,"utf-8");
//				Log.i("wmy", "body = "+body);
//				EntityUtils.consume(entity);
//			}
//			response.close();
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			tv_statusBar_mian.setLayoutParams(layoutParams1);
			tv_statusBar_mian.setVisibility(View.VISIBLE);
		} else {
			tv_statusBar_mian.setVisibility(View.GONE);
		}
	}

	private void setCount(int position) {
		clickMeal = true;

		switch (position) {
		case 1:
			mMealB1.setTextColor(Color.WHITE);
			mMealB2.setTextColor(Color.parseColor("#54cbea"));
			mMealB3.setTextColor(Color.parseColor("#54cbea"));
			mMealB1.setBackgroundResource(R.drawable.btn044);
			mMealB2.setBackgroundResource(R.drawable.btn043);
			mMealB3.setBackgroundResource(R.drawable.btn043);
			count1 = meal1[0];
			count2 = meal1[1];
			count3 = meal1[2];
			count4 = meal1[3];
			count5 = meal1[4];
			for (int i = 0; i < meal1.length; i++) {
				mCountEds.get(i).setText("" + meal1[i]);
				mCountEds.get(i).setSelection(("" + meal1[i]).length());
			}
			break;
		case 2:
			mMealB2.setTextColor(Color.WHITE);
			mMealB1.setTextColor(Color.parseColor("#54cbea"));
			mMealB3.setTextColor(Color.parseColor("#54cbea"));
			mMealB2.setBackgroundResource(R.drawable.btn044);
			mMealB1.setBackgroundResource(R.drawable.btn043);
			mMealB3.setBackgroundResource(R.drawable.btn043);
			count1 = meal2[0];
			count2 = meal2[1];
			count3 = meal2[2];
			count4 = meal2[3];
			count5 = meal2[4];
			for (int i = 0; i < meal2.length; i++) {
				mCountEds.get(i).setText("" + meal2[i]);
				mCountEds.get(i).setSelection(("" + meal2[i]).length());
			}
			break;
		case 3:
			mMealB3.setTextColor(Color.WHITE);
			mMealB2.setTextColor(Color.parseColor("#54cbea"));
			mMealB1.setTextColor(Color.parseColor("#54cbea"));
			mMealB3.setBackgroundResource(R.drawable.btn044);
			mMealB2.setBackgroundResource(R.drawable.btn043);
			mMealB1.setBackgroundResource(R.drawable.btn043);
			count1 = meal3[0];
			count2 = meal3[1];
			count3 = meal3[2];
			count4 = meal3[3];
			count5 = meal3[4];
			for (int i = 0; i < meal3.length; i++) {
				mCountEds.get(i).setText("" + meal3[i]);
				mCountEds.get(i).setSelection(("" + meal3[i]).length());
			}
			break;

		default:
			break;
		}
		// count1 = Integer.parseInt(mCountEd1.getText().toString());
		// count2 = Integer.parseInt(mCountEd2.getText().toString());
		// count3 = Integer.parseInt(mCountEd3.getText().toString());
		// count4 = Integer.parseInt(mCountEd4.getText().toString());
		// count5 = Integer.parseInt(mCountEd5.getText().toString());
	}

	private void resetMealView() {
		clickMeal = false;
		mMealB1.setTextColor(Color.parseColor("#54cbea"));
		mMealB2.setTextColor(Color.parseColor("#54cbea"));
		mMealB3.setTextColor(Color.parseColor("#54cbea"));
		mMealB1.setBackgroundResource(R.drawable.btn043);
		mMealB2.setBackgroundResource(R.drawable.btn043);
		mMealB3.setBackgroundResource(R.drawable.btn043);
	}

}
